package com.seo.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Collections;
import java.util.Date;
import java.util.List;

import org.apache.commons.lang3.StringUtils;

/**
 * 时间工具类
 * 
 * @author Administrator 说明：日期处理 创建人：shibeilei
 * @version 2019.05.10 11:40
 */
public class DateHeiUtil {

	public static SimpleDateFormat FORMAT_TIME = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	public static SimpleDateFormat FORMAT_DATE = new SimpleDateFormat("yyyy-MM-dd");
	public static SimpleDateFormat FORMAT_HMS = new SimpleDateFormat("HH:mm:ss");
	public static SimpleDateFormat FORMAT_YEAR = new SimpleDateFormat("yyyy");
	public static SimpleDateFormat FORMAT_YEAR_M = new SimpleDateFormat("yyyy-MM");
	public static SimpleDateFormat FORMAT_1_TIME = new SimpleDateFormat("yyyyMMddHHmmss");
	public static SimpleDateFormat FORMAT_1_YEAR_M = new SimpleDateFormat("yyyyMM");
	public static SimpleDateFormat FORMAT_1_DATE = new SimpleDateFormat("yyyy-MM-dd");
	public static SimpleDateFormat FORMAT_1_HMS = new SimpleDateFormat("HHmmss");
	public static SimpleDateFormat FORMAT_2_TIME = new SimpleDateFormat("yyyy年MM月dd日 HH时mm分ss秒");
	public static SimpleDateFormat FORMAT_2_YEAR_M = new SimpleDateFormat("yyyy年MM月");
	public static SimpleDateFormat FORMAT_2_DATE = new SimpleDateFormat("yyyy年MM月dd日");
	public static SimpleDateFormat FORMAT_2_2_DATE = new SimpleDateFormat("yy年M月d日");
	public static SimpleDateFormat FORMAT_2_HMS = new SimpleDateFormat("HH时mm分ss秒");

	public static SimpleDateFormat FORMAT_3_YEAR_M = new SimpleDateFormat("yyM");

	public static final int ONE_DAY_MILLISECOND = 86400000;
	public static final long ONE_HOUR_MILLISECOND = 3600000;
	public static final int FORMAT_DAY = 0;

	/**
	 * getCurDate
	 * 
	 * @return 返回Date
	 * @author Administrator
	 * @version 2017.12.01 11:40
	 */
	public static Date getCurDate() {
		return new Date();
	}

	/**
	 * 获取当前时间
	 * 
	 * @return String
	 * @author Administrator
	 * @version 2017.12.01 11:40
	 */
	public static String getCurDate(SimpleDateFormat f) {
		return f.format(new Date());
	}

	public static String getTime() {
		return FORMAT_TIME.format(new Date());
	}

	public static Date getStrToDate(String d, SimpleDateFormat f) {
		try {
			return f.parse(d);
		} catch (ParseException e) {
			e.printStackTrace();
			return null;
		}
	}

	public static String getDateToStr(Date d, SimpleDateFormat f) {

		return f.format(d);
	}

	public static String getStrToStr(String d, SimpleDateFormat f1, SimpleDateFormat f2) {
		Date date;
		try {
			date = f1.parse(d);
			return f2.format(date);
		} catch (ParseException e) {
			e.printStackTrace();
			return null;
		}

	}

	/**
	 * 返回当前时间 增加或减少N(年\月\天\小时\分钟\秒)后的时间
	 * 
	 * @param field 1\2\3\4\5\6
	 * @param add
	 * @return
	 */
	public static Date getAddDate(Date d, int field, int add) {
		Calendar now = Calendar.getInstance();
		if (d != null) {
			now.setTime(d);
		}

		if (field == 1) {
			now.add(Calendar.YEAR, add);
		} else if (field == 2) {
			now.add(Calendar.MONTH, add);
		} else if (field == 3) {
			now.add(Calendar.DAY_OF_MONTH, add);
		} else if (field == 4) {
			now.add(Calendar.HOUR, add);
		} else if (field == 5) {
			now.add(Calendar.MINUTE, add);
		} else if (field == 6) {
			now.add(Calendar.SECOND, add);
		} else {
			return now.getTime();
		}

		return now.getTime();
	}

	public static String getAddDateToStr(int field, int add, SimpleDateFormat f) {
		return f.format(getAddDate(null, field, add));
	}

	public static String getAddDateToStr(Date d, int field, int add, SimpleDateFormat f) {
		return f.format(getAddDate(d, field, add));
	}

	public static Date getAddDate(String s, int field, int add, SimpleDateFormat f) {
		try {
			return getAddDate(f.parse(s), field, add);
		} catch (ParseException e) {
			e.printStackTrace();
			return null;
		}
	}

	public static String getAddDateToStr(String s, int field, int add, SimpleDateFormat f) {
		try {
			return f.format(getAddDate(f.parse(s), field, add));
		} catch (ParseException e) {
			e.printStackTrace();
			return null;
		}
	}

	/**
	 * 比较日期大小
	 * 
	 * @param s 开始时间
	 * @param e 结束时间
	 * @param f 日期格式
	 * @return true false
	 */
	public static int compareDate(String s, String e, SimpleDateFormat f) {
		if (StringUtils.isEmpty(s) || StringUtils.isEmpty(e)) {
			return -2;
		}

		if (getStrToDate(s, f) == null || getStrToDate(e, f) == null) {
			return -2;
		}
		if (getStrToDate(s, f).getTime() > getStrToDate(e, f).getTime()) {
			return 1;
		} else if (getStrToDate(s, f).getTime() == getStrToDate(e, f).getTime()) {
			return 0;
		} else {
			return -1;
		}
	}

	/**
	 * 比较日期大小
	 * 
	 * @param s 开始时间
	 * @param e 结束时间
	 * @return true false
	 */
	public static int compareDate(Date s, Date e) {
		if (s.getTime() > e.getTime()) {
			return 1;
		} else if (s.getTime() == e.getTime()) {
			return 0;
		} else {
			return -1;
		}
	}

	/**
	 * daysOfTwo
	 * 
	 * @param fDate fDate
	 * @param oDate oDate
	 * @return int
	 * @author Administrator
	 * @version 2017.12.01 11:40
	 */
	public static int daysOfTwo(Date fDate, Date oDate) {

		Calendar aCalendarf = Calendar.getInstance();
		Calendar aCalendaro = Calendar.getInstance();

		aCalendarf.setTime(fDate);

		aCalendaro.setTime(oDate);

		int days = ((int) (aCalendaro.getTime().getTime() / 1000) - (int) (aCalendarf.getTime().getTime() / 1000))
				/ 3600 / 24;

		return days;

	}

	public static int daysOfTwo(String fDate, String oDate, SimpleDateFormat f) {

		Calendar aCalendarf = Calendar.getInstance();
		Calendar aCalendaro = Calendar.getInstance();

		aCalendarf.setTime(getStrToDate(fDate, f));

		aCalendaro.setTime(getStrToDate(oDate, f));

		int days = ((int) (aCalendaro.getTime().getTime() / 1000) - (int) (aCalendarf.getTime().getTime() / 1000))
				/ 3600 / 24;

		return days;

	}

	public static String getTimesmorning(SimpleDateFormat f) {
		Calendar cal = Calendar.getInstance();
		cal.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH), cal.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
		Date beginOfDate = cal.getTime();
		return f.format(beginOfDate);
	}

	public static String getTimesmorning(String date, SimpleDateFormat f) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(getStrToDate(date, f));
		cal.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH), cal.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
		Date beginOfDate = cal.getTime();
		return f.format(beginOfDate);
	}

	public static void main(String[] args) {
		String curTime = DateHeiUtil.getTimesmorning(DateHeiUtil.FORMAT_TIME);
		System.out.println(curTime);
		String startTime = DateHeiUtil.getAddDateToStr(curTime, 2, -1, DateHeiUtil.FORMAT_TIME);

		System.out.println(startTime);
		
		String endTime = DateHeiUtil.getAddDateToStr(curTime, 2, 1, DateHeiUtil.FORMAT_TIME);
		
		System.out.println(endTime);
	}
}